<?php
/**
 * licenses : MIT
 * creatTime: 2020/5/13
 * author : weblinuxgame
 * filename: UserService.php
 */

namespace App\Services;

use App\Models\User;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;


/**
 * Class UserService
 * @package App\Services
 */
class UserService extends BaseService
{
    /**
     * 用户创建记录
     * @param array $data
     * @return int
     */
    public function create(array $data = []): int
    {
        if (!isset($data['password'])) {
            return false;
        }
        $data['password'] = encrypt($data['password']);
        $user = new User($data);
        $user->uuid = (new UuidService($user))->get();
        if (empty($user->nickname)) {
            $user->nickname = $user->username;
        }
        if (!empty($user->username) && !empty($user->password) && $user->save()) {
            return $user->id;
        }
        return 0;
    }

    /**
     * 用户
     * @param int $id
     * @return User|null
     */
    public function getById(int $id)
    {
        if ($id <= 0) {
            return null;
        }
        return $this->getByKey('id', $id);
    }

    /**
     * 通过手机号获取用户记录
     * @param string $mobile
     * @return User|null
     */
    public function getByMobile(string $mobile)
    {
        if ($mobile == "") {
            return null;
        }
        return $this->getByKey('mobile', $mobile);
    }

    /**
     * 获取用户通过email
     * @param string $email
     * @return User|null
     */
    public function getByEmail(string $email)
    {
        if ($email == "") {
            return null;
        }
        return $this->getByKey('email', $email);
    }

    /**
     * 获取用户通过uuid
     * @param string $uuid
     * @return User|null
     */
    public function getByUuid(string $uuid)
    {
        if ($uuid == "") {
            return null;
        }
        return $this->getByKey('uuid', $uuid);
    }

    /**
     * 通过唯一键获取用户信息记录
     * @param string $key
     * @param $value
     * @return User|null
     */
    public function getByKey(string $key, $value)
    {
        return User::query()->where($key, $value)->first();
    }


    /**
     * 是否存储对应值
     * @param string $key
     * @param $value
     * @return bool
     */
    public function exists(string $key, $value): bool
    {
        return User::query()->where($key, $value)->exists();
    }

    /**
     * 更新用户数据
     * @param User $user
     * @param array $data
     * @return bool
     */
    protected function update(User $user, array $data): bool
    {
        return $user->update($data);
    }

    /**
     * 更新用户密码
     * @param int $id
     * @param string $password
     * @return bool
     */
    public function updatePassword(int $id, string $password): bool
    {
        $user = $this->getById($id);
        if ($user == null) {
            return false;
        }
        $password = encrypt($password);
        return $this->update($user, ['password' => $password]);
    }

    /**
     * 验证密码是否匹配
     * @param string $encryptPassword
     * @param string $password
     * @return bool
     */
    public static function verifyPassword(string $encryptPassword, string $password): bool
    {
        return decrypt($encryptPassword) == $password;
    }

    /**
     * 更新属性值
     * @param int $id
     * @param array $profiles
     * @param array $options
     * @return bool
     */
    public function setProfiles(int $id, array $profiles, array $options = []): bool
    {
        if (empty($options) || empty($options['withoutDeletedAt'])) {
            $query = User::query();
        } else {
            $query = User::withoutTrashed();
        }
        $user = $query->where('id', $id)->first();
        if (empty($user)) {
            return false;
        }
        foreach ($profiles as $key => $value) {
            $user->{$key} = $value;
        }
        return $user->save();
    }

    /**
     * 获取属性列表
     * @param int $id
     * @param array $profileKeys
     * @return array
     */
    public function getProfiles(int $id, array $profileKeys = []): array
    {
        if (empty($id)) {
            return [];
        }
        $user = $this->user($id);
        if (empty($user)) {
            return [];
        }
        $profiles = $user->toArray();
        if (empty($profileKeys)) {
            return $profileKeys;
        }
        $data = [];
        $all = false;
        if (false !== array_search('*', $profileKeys)) {
            $all = true;
            $data = $profiles;
        }
        foreach ($profileKeys as $key) {
            if (empty($key)) {
                continue;
            }
            if ('!' === $key[0]) {
                if ($all) {
                    Arr::forget($data, substr($key, 1));
                }
                continue;
            }
            if ($all) {
                continue;
            }
            if (Arr::exists($profiles, $key)) {
                Arr::set($data, $key, Arr::get($profiles, $key));
            }
        }
        return $data;
    }

    /**
     * 通过ID用户
     * @param int $id
     * @param bool $withOutDeleted
     * @return User|null
     */
    public function user(int $id, $withOutDeleted = false)
    {
        if (!$withOutDeleted) {
            $query = User::query();
        } else {
            $query = User::withoutTrashed();
        }
        return $query->where('id', $id)->first();
    }

}
